package com.sdgakj.web.feedback;

import com.google.common.collect.Maps;
import com.sdgakj.dto.base.ResCode;
import com.sdgakj.dto.base.ResResult;
import com.sdgakj.entity.Feedback;
import com.sdgakj.enums.EnumIsDelete;
import com.sdgakj.service.ServiceException;
import com.sdgakj.service.feedback.FeedbackService;
import com.sdgakj.utils.LoginAdminUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.util.Map;

@Controller
@RequestMapping(value="/feedback")
public class FeedbackController {
	
	private static Logger logger = LoggerFactory.getLogger(FeedbackController.class);
	
	@Autowired
	private FeedbackService feedbackService;
	
	private static final String PAGE_SIZE = "10";
	
	private static Map<String,String> sortTypes =Maps.newLinkedHashMap();
	
	static {
		sortTypes.put("auto", "自动");
	}

	/**
	 * 查询反馈列表
	 * @param pageNumber 页号
	 * @param pageSize   页数
	 * @param sortType   排序字段
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model,
			ServletRequest request){
		Map<String,Object> searchParams =Servlets.getParametersStartingWith(request, "search_");
//		if(searchParams.size()<1){
//			searchParams.put("LIKE_deltag", "0");
//		}
		Page<Feedback> feedbacks =feedbackService.getFeedbackByPage(searchParams,pageNumber,pageSize,sortType);
		
		model.addAttribute("feedbacks",feedbacks);
		model.addAttribute("sortType", sortType);
		model.addAttribute("sortTypes", sortTypes);
		// 将搜索条件编码成字符串，用于排序，分页的URL
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
		return "feedback/feedbackList";
	}
	/**
	 * 删除反馈信息
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)
	public String delete(@PathVariable("id") Long id,RedirectAttributes redirectAttributes){
		Feedback feedback =feedbackService.getFeedbackById(id);
		feedback.setDeltag(EnumIsDelete.DELETED.getCode()); //设置已经删除
		feedbackService.saveFeedback(feedback);
		redirectAttributes.addFlashAttribute("message", "已标记为无用信息！");
		return "redirect:/feedback/list";
	}
	
	/**
	 * 更新反馈信息
	 * @param entity
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public String update(@Valid @ModelAttribute("feedback")Feedback entity, RedirectAttributes redirectAttributes){
		feedbackService.updateFeedback(entity);
		redirectAttributes.addFlashAttribute("message", "更新信息成功");
		return "redirect:/feedback/list";
	}
	
	/**
	 * AJA处理反馈
	 * 
	 * @param feedbackId
	 * @param review
	 * @return
	 */
	@RequestMapping(value = "review", method = RequestMethod.POST)
	@ResponseBody
	public ResResult review(Long feedbackId, String review) {
		ResResult result = new ResResult();
		try {
			feedbackService.reviewFeedback(feedbackId, LoginAdminUtil.getCurrentUserId(), review);
		} catch (ServiceException e) {
			result.setResCode(e.getResCode());
			logger.info("W-[处理用户反馈]业务错误", e);
		} catch (Exception e) {
			result.setResCode(ResCode.ERROR);
			logger.error("W-[处理用户反馈]程序异常", e);
		}
		return result;
	}
	
	@ModelAttribute
	public void getFeedback(@RequestParam(value ="id",defaultValue = "-1")Long id ,Model model){
		if(id != -1){
			model.addAttribute("feedback", feedbackService.getFeedbackById(id));
		}
	}
}
